package com.trt.beibei.api.v1;

import com.trt.beibei.bo.PageCounter;
import com.trt.beibei.exception.http.NotFoundException;
import com.trt.beibei.model.Spu;
import com.trt.beibei.service.SpuService;
import com.trt.beibei.util.CommonUtil;
import com.trt.beibei.vo.PagingDozer;
import com.trt.beibei.vo.SpuSimplifyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

/**
 * @author tanrentao
 * @desc 商品控制器
 */
@RestController
@RequestMapping("/spu")
@Validated
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * Spu详情数据查询
     * @param id
     */
    @GetMapping("/id/{id}/detail")
    public Spu getDetail(@PathVariable @Positive Long id) {
        Spu spu = spuService.getSpu(id);
        if (spu == null) {
            throw new NotFoundException(30003);
        }
        return spu;
    }

    @GetMapping("/id/{id}/simplify")
    public SpuSimplifyVO getSimplifySpu(@PathVariable @Positive(message = "{id.positive}") Long id) {
        Spu spu = spuService.getSpu(id);
        SpuSimplifyVO simplifyVO = new SpuSimplifyVO();
        BeanUtils.copyProperties(spu, simplifyVO); // BeanUtils缺点 不好处理数组

        return simplifyVO;
    }

    /**
     * 查询商品列表类数据
     * localhost:8081/v1/spu/latest
     */
//    @GetMapping("/latest")
//    public List<SpuSimplifyVO> getLatestSpuList() {
//        Mapper mapper = DozerBeanMapperBuilder.buildDefault(); // DozerBeanMapper优势可以深拷贝
//        List<Spu> spuList = spuService.getLatestPagingSpu();
//        List<SpuSimplifyVO> spuSimplifyVOList = new ArrayList<>();
//        spuList.forEach(s->{
//            SpuSimplifyVO spuSimplifyVO = mapper.map(s, SpuSimplifyVO.class);
//            spuSimplifyVOList.add(spuSimplifyVO);
//        });
//        return spuSimplifyVOList;
//    }
    @GetMapping("/latest")
    public PagingDozer<Spu, SpuSimplifyVO> getLatestSpuList(@RequestParam(defaultValue = "0") Integer start,
                                                @RequestParam(defaultValue = "10") Integer count) { //合理20

        PageCounter pageCounter = CommonUtil.convertToPageParameter(start, count);
        Page<Spu> page = spuService.getLatestPagingSpu(pageCounter.getPage(), pageCounter.getCount());

        return new PagingDozer<>(page, SpuSimplifyVO.class);
    }

    /**
     * 根据分类Id查询商品列表
     * @param id
     * @param isRoot
     * @param start
     * @param count
     */
    @GetMapping("/by/category/{id}")
    public PagingDozer<Spu, SpuSimplifyVO> getByCategoryId(@PathVariable @Positive Long id,
                                                           @RequestParam(name = "is_root", defaultValue = "false") Boolean isRoot,
                                                           @RequestParam(name = "start", defaultValue = "0") Integer start,
                                                           @RequestParam(name = "count", defaultValue = "10") Integer count) {

        PageCounter pageCounter = CommonUtil.convertToPageParameter(start, count);
        Page<Spu> page = spuService.getByCategory(id, isRoot, pageCounter.getPage(), pageCounter.getCount());
        return new PagingDozer<>(page, SpuSimplifyVO.class);
    }


}
